MY_CFLAGS=-Og -g -DDEBUG
ifneq ($(KERNELRELEASE),)
	obj-m := scullc.o
	ccflags-y += $(MY_CFLAGS)
else
	#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	KERNELDIR ?= /home/sam/sandbox/linux
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules EXTRA_CFLAGS="$(MY_CFLAGS)"
endif

scullc.o: scullc.c

run: default
	qemu-system-x86_64 -enable-kvm -cpu host -smp 4 \
	-m 256M \
	-nographic \
	-kernel ../../linux/arch/x86_64/boot/bzImage \
	-initrd ../initramfs.tgz \
	-fsdev local,id=ldd,path=$(PWD),security_model=mapped,multidevs=remap \
	-device virtio-9p-pci,fsdev=ldd,mount_tag=ldd \
	-append "nokaslr console=ttyS0 vm_inst=1 kgdbwait kgdboc=ttyS1,115200"

# After running, you can mount the fsdev
# mkdir -p /lib/modules/`uname -r`
# mount -t 9p -o trans=virtio ldd /lib/modules/`uname -r`

debug: default
	qemu-system-x86_64 -enable-kvm -cpu host -smp 1 \
	-m 256M \
	-nographic \
	-kernel ../../linux/arch/x86_64/boot/bzImage \
	-initrd ../initramfs.tgz \
	-fsdev local,id=ldd,path=$(PWD),security_model=mapped,multidevs=remap \
	-device virtio-9p-pci,fsdev=ldd,mount_tag=ldd \
	-append "nokaslr console=ttyS0 vm_inst=1" \
	-s -S

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean

.PHONY: default run debug clean
#	-chardev socket,id=gdb,port=1234,server,nowait,host=0.0.0.0 \
#	-device pci-serial,chardev=gdb \
#	-append "nokaslr console=ttyS0 vm_inst=1 kgdbwait kgdboc=ttyS1,115200"
